package demo.um.controller;

import java.io.Serializable;
import java.util.List;
import java.util.logging.Logger;

import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.http.HttpSession;

import org.primefaces.component.datatable.DataTable;
import org.primefaces.event.RowEditEvent;

import demo.um.model.UserInfo;
import demo.um.service.UserService;

@Named
@SessionScoped
public class UserController implements Serializable {

	private static final long serialVersionUID = 5831436545217285514L;

	@Inject
	private Logger log;

	@Inject
	private UserService userService;

	private List<UserInfo> users;

	private DataTable userList;

	private String pet = "puppy";

	private Boolean selected = false;

	private UserInfo selectedUser;
	private UserInfo newUser = new UserInfo();

	@PostConstruct
	private void init() {
		log.info("----------------------------PostConstruct...");
	}

	private void printSession() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		HttpSession httpSession = (HttpSession) facesContext.getExternalContext().getSession(true);
		log.info("FacesContext PhaseId: " + FacesContext.getCurrentInstance().getCurrentPhaseId());
		log.info("PartialViewContext: " + facesContext.getPartialViewContext().getExecuteIds() + " is Ajax request: "
				+ facesContext.getPartialViewContext().isAjaxRequest());
		log.info("FacesContext Session ID: " + httpSession.getId());
	}

	public List<UserInfo> getUsers() {
		Integer.valueOf("abc");
		
		log.info("-------------------------getUsers()-------------------------");
		printSession();
		users = userService.getUsers();
		return users;
	}

	public void addUser() {
		log.info("-------------------------addUsers()-------------------------");
		printSession();
		userService.saveUser(newUser);
		newUser = new UserInfo();
	}

	public void editUser() {
		userService.saveUser(selectedUser);
	}

	public void onEditUser(RowEditEvent e) {
		UserInfo user = (UserInfo) e.getObject();
		userService.saveUser(user);

		FacesMessage msg = new FacesMessage("User Edited", user.getName());
		FacesContext.getCurrentInstance().addMessage(null, msg);
	}

	public void deleteUser() {
		userService.deleteUser(selectedUser);
		users.remove(selectedUser);
	}

	public void cleanSelectedUser() {
		log.info("-------------------------clean SelectedUser-------------------------");
		selectedUser = null;
	}

	public UserInfo getSelectedUser() {
		log.info("-------------------------get SelectedUser-------------------------");
		printSession();
		return selectedUser;
	}

	public void setSelectedUser(UserInfo selectedUser) {
		log.info("-------------------------set SelectedUser-------------------------");
		printSession();
		this.selectedUser = selectedUser;
	}

	public UserInfo getNewUser() {
		log.info("-------------------------get NewUser-------------------------");
		printSession();
		return newUser;
	}

	public void setNewUser(UserInfo newUser) {
		this.newUser = newUser;
	}

	public DataTable getUserList() {
		return userList;
	}

	public void setUserList(DataTable userList) {
		this.userList = userList;
	}

	public String getPet() {
		return pet;
	}

	public void setPet(String pet) {
		this.pet = pet;
	}

	public boolean isSelected() {
		return selected;
	}

	public void setSelected(boolean selected) {
		this.selected = selected;
	}

}
